package purchaseStore

import (
	"context"
	"github.com/google/uuid"
	"insighttracking/apps/purchase/auth"
	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"
	types2 "insighttracking/common/types"
	"insighttracking/query/service"
	"net/http"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetPurchaseShelfListApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetPurchaseShelfListApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetPurchaseShelfListApiLogic {
	hostname, _ := os.Hostname()
	return &GetPurchaseShelfListApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetPurchaseShelfListApiLogic) GetPurchaseShelfListApi(req *types.WarehouseID) (resp *types.CommonList, err error) {
	resp = &types.CommonList{}
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return
	}
	entities, err := service.ShelfService.GetListByWarehouseIDApi(l.ctx, accountInfo.EnterpriseId, req.WarehouseID)
	if err != nil {
		return
	}
	list := make([]*types2.SimpleList, 0)
	for _, entity := range entities {
		list = append(list, &types2.SimpleList{
			ID:   entity.ID,
			Name: entity.Name,
			Code: entity.Code,
		})
	}
	resp.List = list
	return
}
